﻿using System;
using System.Web.Mvc;
using INSE6260.OnlineBanking.Model.Transaction;
using INSE6260.OnlineBanking.Service.Interfaces;
using INSE6260.OnlineBanking.Service.Messaging.ClientService;
using INSE6260.OnlineBanking.Web.MVC.Helpers;
using System.Linq;
namespace INSE6260.OnlineBanking.Web.MVC.Controllers
{
    public class TransactionHistoryController : Controller
    {
        private readonly ITransactionRepository _transactionRepository;
        private readonly IClientService _clientService ;
        public TransactionHistoryController(ITransactionRepository transactionRepository, IClientService clientService)
        {
            _transactionRepository = transactionRepository;
            _clientService = clientService;
        }
        public ActionResult Show()
        {
            SetDate("", "");
            ViewBag.FromDate = fromDate.ToString("dd/MM/yyyy");
            ViewBag.ToDate = toDate.AddDays(-1).ToString("dd/MM/yyyy");
            var clientView = _clientService.GetClient(new GetClientRequest { ClientIdToken = UserProvider.ActiveClient.ClientId, LoadAccounts = true });
            if (clientView != null)
            {
                ViewBag.Accounts = new SelectList(clientView.Accounts, "AccountId", "AcountDescAndNo");
                if(clientView.Accounts != null && clientView.Accounts.Any())
                {
                    var model = _transactionRepository.GetTransactionHistory(clientView.Accounts.ToList()[0].AccountId, fromDate, toDate);
                    return View("Index", model);
                }
            }
            
            
            return View("Index");
        }
        public ActionResult Index(int accountId)
        {
            var clientView = _clientService.GetClient(new GetClientRequest { ClientIdToken = UserProvider.ActiveClient.ClientId, LoadAccounts = true });
            if(clientView != null)
            {
                ViewBag.Accounts = new SelectList(clientView.Accounts, "AccountId", "AcountDescAndNo", accountId);
            }
            ViewBag.AccountId = accountId;
            SetDate("", "");
            ViewBag.FromDate = fromDate.ToString("dd/MM/yyyy");
            ViewBag.ToDate = toDate.AddDays(-1).ToString("dd/MM/yyyy");
            var model = _transactionRepository.GetTransactionHistory(accountId, fromDate, toDate);
            return View(model);
        }
        DateTime toDate;
        DateTime fromDate;
        public ActionResult ShowHistory(string startDate, string endDate,int accountId)
        {
           
            SetDate(startDate, endDate);
            if (!ModelState.IsValid)
                return PartialView("TransactionHistoryList",null);

           var model = _transactionRepository.GetTransactionHistory(accountId, fromDate, toDate);
            return PartialView("TransactionHistoryList", model);
        }

        private void SetDate(string startDate, string endDate)
        {
            if (String.IsNullOrEmpty(startDate))
                fromDate = DateTime.Now.AddDays(-(DateTime.Now.Day+1));
            else if (!DateTime.TryParse(startDate, out fromDate))
                ModelState.AddModelError("", "From Date is invalid.");
            if (String.IsNullOrEmpty(endDate))
                toDate = DateTime.Now;
            else if (!DateTime.TryParse(endDate, out toDate))
                ModelState.AddModelError("", "To Date is invalid.");
            toDate = toDate.Date.AddDays(1);

        }
    }
}
